package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.UserApexInfoMapper;
import cc.rengu.igas.bomp.common.entity.UserApexInfo;
import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户附加信息表操作接口实现类
 * Created by wangcheng on 2021/7/19 14:44
 */
public class UserApexInfoMapperImpl implements UserApexInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    public UserApexInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppConfigConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertUserApexInfo(UserApexInfo userApexInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertUserApexInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, userApexInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记用户附加信息失败,instId:<{}>,userId:<{}>,propertyKey:<{}>,propertyValue:<{}>,iReturnCode:<{}>",
                    userApexInfo.getInstId(), userApexInfo.getUserId(), userApexInfo.getPropertyKey(), userApexInfo.getPropertyValue(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateUserApexInfoByPrimaryKey(UserApexInfo userApexInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateUserApexInfoByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, userApexInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据业务主键更新用户附加信息失败,instId:<{}>,userId:<{}>,propertyKey:<{}>,propertyValue:<{}>,iReturnCode:<{}>",
                    userApexInfo.getInstId(), userApexInfo.getUserId(), userApexInfo.getPropertyKey(), userApexInfo.getPropertyValue(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public UserApexInfo selectUserApexInfoByPrimaryKey(String instId, String userId, String userType, String propertyKey) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserApexInfoByPrimaryKey");
        UserApexInfo userApexInfo = new UserApexInfo();
        userApexInfo.setInstId(instId);
        userApexInfo.setUserId(userId);
        userApexInfo.setUserType(userType);
        userApexInfo.setPropertyKey(propertyKey);
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, userApexInfo, userApexInfo);
        if (0 != iReturnCode) {
            rglog.error("根据业务主键查询用户附加信息失败,instId:<{}>,userId:<{}>,userType:<{}>,propertyKey:<{}>,iReturnCode:<{}>", instId, userId, userType, propertyKey, iReturnCode);
            return null;
        }
        return userApexInfo;
    }

    @Override
    public List<UserApexInfo> selectUserApexInfoByUserId(String instId, String userId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectUserApexInfoByUserId");
        UserApexInfo userApexInfo = new UserApexInfo();
        userApexInfo.setInstId(instId);
        userApexInfo.setUserId(userId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, userApexInfo, UserApexInfo.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据用户标识查询用户附加信息失败，instId:<{}>,userId:<{}>", instId, userId);
            return null;
        }
        return objectList.stream().map(item -> (UserApexInfo) (item)).collect(Collectors.toList());
    }
}
